package com.liusand.user.controller;

import com.liusand.entity.dto.PhoneAndVerifyCodeDto;
import com.liusand.entity.dto.UserDto;
import com.liusand.entity.pojo.User;
import com.liusand.entity.vo.*;
import com.liusand.user.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author :liusha
 * packageName :com.liusand.user.controller
 * fileName :UserController
 * creationTime :2022/12/10 20:28
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("/sendSms")
    public Result sendSms(@RequestBody PhoneAndVerifyCodeDto phone){
        if(phone == null || StringUtils.isEmpty(phone.getPhone())){
            return Result.error("发送短信失败");
        }
        boolean sendResult = userService.sendSms(phone);
        if(!sendResult){
            return Result.error("发送短信失败");
        }
        return Result.success();
    }
    @PostMapping("/login")
    public Result login(@RequestBody PhoneAndVerifyCodeDto phoneAndCode){
        if(phoneAndCode == null || StringUtils.isEmpty(phoneAndCode.getPhone()) || StringUtils.isEmpty(phoneAndCode.getVerifyCode())){
            return Result.error("登录失败");
        }
        LoginVo loginVo = userService.login(phoneAndCode);
        if(loginVo == null || StringUtils.isEmpty(loginVo.getToken()) || loginVo.getFirstLogin() == null){
            return Result.error("登录失败");
        }
        return Result.success(loginVo);
    }
    @PutMapping("/modifyInfo")
    public Result modifyInfo(@RequestBody UserDto userDto){
        if(userDto == null || StringUtils.isEmpty(userDto.getBirthday())
            || StringUtils.isEmpty(userDto.getCity())
            || StringUtils.isEmpty(userDto.getNickname())
            || userDto.getGender() == null){
            return Result.error("更新资料失败");
        }
        boolean modifyResult = userService.modifyInfo(userDto);
        if(!modifyResult){
            return Result.error("更新资料失败");
        }
        return Result.success();
    }
    @PostMapping("/logout")
    public Result logout(){
        return Result.success();
    }

    @GetMapping("/getList")
    public Result getList(@RequestParam Integer type,
                          @RequestParam(required = false) String value,
                          @RequestParam(defaultValue = "1") Integer page,
                          @RequestParam(defaultValue = "10") Integer pageSize){
        PageInfo<ListUserVo> pageInfo = userService.getList(type, value, page, pageSize);
        return Result.success(pageInfo);
    }
    @GetMapping("/getList/count")
    public Result countGetList(@RequestParam Integer type,
                               @RequestParam(required = false) String value){
        Long num = userService.countGetList(type, value);
        return Result.success(num);
    }

    @GetMapping("/relationHandle")
    public Result relationHandle(@RequestParam Integer type,
                                  @RequestParam String toId){
        boolean result = userService.relationHandle(type, toId);
        return result ? Result.success() : Result.error("操作失败");
    }

    @GetMapping("/getInfo")
    public Result getInfo(){
        User user = userService.getInfo();
        if(user == null){
            return Result.error("查询异常");
        }
        return Result.success(user);
    }

    @GetMapping("getVisitedUserVo")
    public Result getVisitedUserVo(@RequestParam(defaultValue = "1") Integer page,
                                   @RequestParam(defaultValue = "10") Integer pageSize){
        List<UserVo> userVoList = userService.getVisitedUserVo(page, pageSize);
        return Result.success(userVoList);
    }

}
